package com.example.xm.service.impl;

import com.example.xm.entity.DingCai;
import com.example.xm.entity.Geci;
import com.example.xm.entity.Geshou;
import com.example.xm.entity.Yinyue;
import com.example.xm.entity.vo.YinYueVo;
import com.example.xm.mapper.DingcaiMapper;
import com.example.xm.mapper.GeciMapper;
import com.example.xm.mapper.YinyueMapper;
import com.example.xm.service.IYinyueService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class YinyueServiceImpl implements IYinyueService {
    @Autowired
    private YinyueMapper yinyueMapper;
@Autowired
    GeciMapper geciMapper;
    @Override
    public PageInfo<Yinyue> selectList(Yinyue yinyue) {
        PageHelper.startPage(yinyue.getPage(),yinyue.getSize());
        List<Yinyue> geshous = yinyueMapper.selectList(yinyue);
        PageInfo<Yinyue> adminPageInfo = new PageInfo<Yinyue>(geshous);
        return adminPageInfo;
    }

    @Override
    public int add(Yinyue yinyue) {
        yinyue.setDianzan(0);
        yinyue.setNum(0);
        yinyue.setCreate_time(new Date());
        return yinyueMapper.insertSelective(yinyue);
    }

    @Override
    public int edit(Yinyue yinyue) {
        return yinyueMapper.updateByPrimaryKeySelective(yinyue);
    }

    @Override
    public void delete(int[] intArray) {
        yinyueMapper.delete(intArray);
    }
@Autowired
    DingcaiMapper dingcaiMapper;
    @Override
    public YinYueVo getDetail(Integer vid,Integer uid) {
        YinYueVo yinYueVo = yinyueMapper.getDetail(String.valueOf(vid));
        Geci geci = new Geci();
        geci.setVid(Integer.valueOf(vid));
        List<Geci> gecis = geciMapper.selectList(geci);
        yinYueVo.setGeciList(gecis);
        List<DingCai> list = dingcaiMapper.selectList(uid, vid, 1);
        if(list.size()>0){
            yinYueVo.setIsdianzan(true);
        }else{
            yinYueVo.setIsdianzan(false);
        }
        List<DingCai> list2 = dingcaiMapper.selectList(uid, vid, 2);
        if(list2.size()>0){
            yinYueVo.setIsshoucang(true);
        }else{
            yinYueVo.setIsshoucang(false);
        }
        return yinYueVo;
    }

    @Override
    public List<Yinyue> getTuijian(Integer uid) {
        List<Yinyue> list=null;
        if(uid!=null &&!uid.equals("")){
            //拿到用户的点赞记录分类
            List<Integer> categoryList=   dingcaiMapper.selectCategoryList(uid);
            //拿到点赞过此分类的用户
            List<Integer> userList=   dingcaiMapper.selectUserList(categoryList);
            //拿到分类
            List<Integer> cids=   dingcaiMapper.selectCidList(userList);
            list=yinyueMapper.selectListByCids(cids);
            if(list.size()==0){
                list = yinyueMapper.selectList(null);
            }
        }else{
            list = yinyueMapper.selectList(null);
        }
        return list;
    }
}
